package org.ru.mse10.cvis.web.bean.navigation;

import java.util.ResourceBundle;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.ru.mse10.cvis.constants.UserRole;
import org.ru.mse10.cvis.entity.cv.User;
import org.ru.mse10.cvis.web.bean.BaseUIBean;

/**
 * The Class NavigationBean.
 */
@SessionScoped
@ManagedBean(name = "navigationBean")
public class NavigationBean extends BaseUIBean {

	/** The error message. */
	private String errorMessage;

	/** The context. */
	private FacesContext context = FacesContext.getCurrentInstance();

	/** The bundle. */
	private ResourceBundle bundle = context.getApplication().getResourceBundle(
			context, "message");

	/** The area. */
	private NavigationArea area = NavigationArea.HOME;

	/**
	 * Action switch menu area.
	 * 
	 * @param area
	 *            the area
	 * @return the string
	 */
	public String actionSwitchMenuArea(String area) {
		this.area = NavigationArea.valueOf(area);
		return this.area.getDefaultAreaNavigation();
	}

	/**
	 * Checks if is your cv area selected.
	 * 
	 * @return true, if is your cv area selected
	 */
	public boolean isYourCvAreaSelected() {
		if (isLoggedIn() == true) {
			errorMessage = "";
			return NavigationArea.YOUR_CV == area;
		} else {
			errorMessage = bundle.getString("pleaseLogin");
		}
		return false;
	}

	/**
	 * Checks if is manage cvs selected.
	 * 
	 * @return true, if is manage cvs selected
	 */
	public boolean isManageCvsSelected() {
		if (isLoggedIn() == true) {
			errorMessage = "";
			return NavigationArea.MANAGE_CVS == area;
		} else {
			errorMessage = bundle.getString("pleaseLogin");
		}
		return false;
	}

	/**
	 * Checks if is home selected.
	 * 
	 * @return true, if is home selected
	 */
	public boolean isHomeSelected() {
		return NavigationArea.HOME == area;
	}

	/**
	 * Checks if is admin selected.
	 * 
	 * @return true, if is admin selected
	 */
	public boolean isAdminSelected() {
		if (isLoggedIn() == true) {
			User user = getLoggedUser();
			if (user.getRole() == UserRole.ADMIN) {
				errorMessage = "";
				return NavigationArea.ADMIN == area;
			} else {
				errorMessage = bundle.getString("adminRights");
			}
		} else {
			errorMessage = bundle.getString("pleaseLogin");
		}
		return false;
	}

	/**
	 * Checks if is help selected.
	 * 
	 * @return true, if is help selected
	 */
	public boolean isHelpSelected() {
		return NavigationArea.HELP == area;
	}

	/**
	 * Gets the error message.
	 * 
	 * @return the error message
	 */
	public String getErrorMessage() {
		return errorMessage;
	}

	/**
	 * Sets the error message.
	 * 
	 * @param errorMessage
	 *            the new error message
	 */
	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}
}
